﻿@using System.Collections
@using Telerik.Web.Mvc.UI
@using Helena.Areas.Ventas.Models
@using Helena.Areas.Configuraciones.Models
@using Helena.Areas.Productos.Models
@using Helena.Areas.Proveedores.Models
@using Helena.Areas.Campanas.Models
@using Helena.Areas.Categorias.Models

@model IEnumerable<DetalleVentaView>  

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
        
@{
    ViewBag.Title = "Detalle de Venta";
    Layout = "~/Views/Shared/_Layout.cshtml";
    VentaView venta = (VentaView)Session["NuevaVenta"];
}

@Html.Telerik().ScriptRegistrar().Globalization(true)

<script type="text/javascript">
    var Nombre;
    function onRowSelectedCampanas(e) {
        var DetalleVentaGrid = $('#Productos').data('tGrid');
        Nombre = e.row.cells[0].innerHTML;
        $('#_Nombre').text(Nombre);
        DetalleVentaGrid.rebind();
    }

    function onRowSelectedCategorias(e) {
        var DetalleVentaGrid = $('#Productos').data('tGrid');
        Nombre = e.row.cells[0].innerHTML;
        $('#_Nombre').text(Nombre);
        DetalleVentaGrid.rebind();
    }

    function onDataBinding(e) {
        e.data = $.extend(e.data, { _Nombre: Nombre });
    }


    function OnSubmitChanges(e) {
        var detallesGrid = $('#Detalles').data('tGrid');
        detallesGrid.rebind();
    }
    
    function onDataBindingDetalle(e) {
        e.data = $.extend(e.data, { VentaID: @(venta.ID)});
    }

    function updateShouts() {
        $('#resumen').load('/Ventas/DetalleVenta/cantidad');
    }
    setInterval("updateShouts()", 1000);
</script> 

<div class="block big">
    <!-- Block Begin -->
    <!--Datos del comproducto compuesto -->
<div id="resumen">@{Html.RenderAction("cantidad", "DetalleVenta");}</div>

<table style="width:100%">
    <tr>
        <td>
            <table>
                <tr>
                    <td>
                        <h3>Categorias</h3>
                            @(Html.Telerik().Grid((IEnumerable<CategoriaView>)ViewData["Categorias"])
                            .Name("Categorias")
                            .Pageable(paging => paging
                                        .PageSize(13)
                                        .Style(GridPagerStyles.Status).PageOnScroll(true))
                            .Columns(columns =>
                            {
                                columns.Bound(c => c.NombreCategoria);
                            })
                            .DataBinding(dataBinding => dataBinding.Ajax().Select("ObtenerCategorias", "DetalleVenta"))
                            .Filterable()
                            .Pageable()
                            .Sortable()
                            .Selectable()
                            .Scrollable(scrolling => scrolling.Height(200))
                            .ClientEvents(events => events.OnRowSelect("onRowSelectedCategorias"))
                            .RowAction(row => 
                            {
                                row.Selected = row.DataItem.NombreCategoria.Equals(ViewData["NombreCategoria"]);
                            })

                            )
                    </td>                
                </tr>
                <tr>
                    <td>
                        <h3>Campañas</h3>
                            @(Html.Telerik().Grid((IEnumerable<CampanaView>)ViewData["Campanas"])
                            .Name("Campanas")
                                    .Pageable(paging => paging
                                                .PageSize(13)
                                                .Style(GridPagerStyles.Status).PageOnScroll(true))                
                            .Columns(columns =>
                            {
                                columns.Bound(c => c.NombreCampana);
                            })
                            .DataBinding(dataBinding => dataBinding.Ajax().Select("ObtenerCampanas", "DetalleVenta"))
                            .Filterable()
                            .Pageable()
                            .Sortable()
                            .Selectable()
                            .Scrollable(scrolling => scrolling.Height(200))
                            .ClientEvents(events => events.OnRowSelect("onRowSelectedCampanas"))
                            .RowAction(row => 
                            {
                                row.Selected = row.DataItem.NombreCampana.Equals(ViewData["_Nombre"].ToString());
                            })

                            )
                    </td>                
                </tr>
            </table>
        </td>
        <td>
            <table style="width:100%">
                <tr>
                    <td>
                        <h3>Productos </h3>
                            @(Html.Telerik().Grid((IEnumerable<ProductoView>)ViewData["Productos"])
                            .Name("Productos")
                            .Pageable(paging => paging
                                .PageSize(20)
                                .Style(GridPagerStyles.Status).PageOnScroll(true))
                            .Localizable("es-ES")
                            .DataKeys(keys => keys.Add(c => c.ID))
                            .Columns(columns=>
                            {
                                columns.Bound(c => c.Descripcion);
                                columns.Bound(c => c.cantidad);
                                columns.Bound(c => c.precioventa).Format("{0:c}");
                                columns.Command(commands =>
                                {
                                    commands.Edit().ButtonType(GridButtonType.BareImage);
                                }).Title("Agregar");
                            })
                            .DataBinding(dataBinding => dataBinding
                                .Ajax()
                                .Select("Obtener_Productos", "DetalleVenta", new { _Nombre = "Anual" })
                                .Update("InsertarDetalleVenta", "DetalleVenta"))
                
                            .ClientEvents(clientEvents => clientEvents.OnDataBinding("onDataBinding"))
                            .ClientEvents(events => events.OnComplete("OnSubmitChanges"))            
                            .Scrollable(scrolling => scrolling.Height(300))
                            .Pageable()
                            .Filterable()
                            .Sortable()
                            .Selectable()
                            )
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Detalles </h3>
                        @(Html.Telerik().Grid(Model)
                        .Name("Detalles")
                        .Localizable("es-ES")
                        .DataKeys(keys => keys.Add(x => x.ID))
                                        .Editable(editing => editing
                                        .Mode(GridEditMode.InLine)
                                ) 
                        .Columns(columns=>
                        {
                            columns.Bound(x => x.NombreProducto);
                            columns.Bound(x => x.Cantidad);
                            columns.Bound(x => x.PrecioU).Format("{0:c}");
                            columns.Bound(x => x.Igv_del_Subtotal).Format("{0:c}");
                            columns.Bound(x => x.Subtotal).Format("{0:c}");
                            columns.Command(commands =>
                            {
                                commands.Delete().ButtonType(GridButtonType.BareImage);
                                commands.Edit().ButtonType(GridButtonType.BareImage);
                            }).Width(80).Title("Modificar");
                        })
                        .DataBinding(dataBinding => dataBinding
                            .Ajax()
                            .Select("ObtenerDetalles", "DetalleVenta", new { VentaID = ((VentaView)Session["NuevaVenta"]).ID })
                            .Delete("EliminarDetalle", "DetalleVenta")
                            .Update("ModificarDetalles", "DetalleVenta")
                            )
                            
                        .ClientEvents(clientEvents => clientEvents.OnDataBinding("onDataBindingDetalle"))
                        .Scrollable(scrolling => scrolling.Height(100))
                        .Pageable()
                        .Filterable()
                        .Sortable()                    
                        )
                    </td>
                </tr>
        </table>
        </td>
    </tr>
</table>

</div>
















